package com.itouch.bracelet;
/**
 * sunshineHu
 */

import java.util.Iterator;
import java.util.List;

import io.rong.imkit.RongIM;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class BaseApplication extends Application{

	public static final String APP_KEY = "8luwapkvu0dbl";
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		initImageLoader(getApplicationContext());

		int pid = android.os.Process.myPid();
		String processAppName = getCurProcessName(pid);

		if (processAppName != null &&processAppName.equals("com.itouch.bracelet")) {
			RongIM.init(this, APP_KEY, R.drawable.ic_launcher);
		}

	}

	private String getCurProcessName(int pID) {
		String processName = null;
		ActivityManager am = (ActivityManager) this
				.getSystemService(ACTIVITY_SERVICE);
		List l = am.getRunningAppProcesses();
		Iterator i = l.iterator();
		PackageManager pm = this.getPackageManager();
		while (i.hasNext()) {
			ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i
					.next());
			try {
				if (info.pid == pID) {
					CharSequence c = pm.getApplicationLabel(pm
							.getApplicationInfo(info.processName,
									PackageManager.GET_META_DATA));
					processName = info.processName;
					return processName;
				}
			} catch (Exception e) {
			}
		}
		return processName;
	}

	/**
	 * 初始化imagerloader
	 * by sj
	 * @param context
	 */
	public static void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)  //设置线程的优先级
				.denyCacheImageMultipleSizesInMemory()    //当同�?个Uri获取不同大小的图片，缓存到内存时，只缓存�?个�?�默认会缓存多个不同的大小的相同图片
				.discCacheFileNameGenerator(new Md5FileNameGenerator())   //设置文件缓存的名�?  Md5FileNameGenerator()：�?�过Md5将url生产文件的唯�?名字
				.tasksProcessingOrder(QueueProcessingType.LIFO)   //设置图片下载和显示的工作队列排序 
				.writeDebugLogs() 
				// -----------------以下可以选择设置----------------------
				//.discCacheFileCount(100);    //设置缓存文件的数�?
				
				.build();
		ImageLoader.getInstance().init(config);
	}


}
